package com.carl.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.carl.mall.product.vo.AttrEntityVO;
import com.carl.mall.product.vo.AttrResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.carl.mall.product.entity.AttrEntity;
import com.carl.mall.product.service.AttrService;
import com.carl.common.utils.PageUtils;
import com.carl.common.utils.R;



/**
 * 商品属性
 *
 * @author carl
 * @email 541020344@qq.com
 * @date 2024-03-10 00:31:47
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    //http://localhost:9006/app/product/attr/base/list/0?t=1717931509778&page=1&limit=10&key=
    /**
     * 规格参数 和 销售属性
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId
                      ,@PathVariable("attrType") String attrType){
        PageUtils pageUtils = attrService.queryByPage(params, catelogId,attrType);
        return R.ok().put("page",pageUtils);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrResponseVO attrResponseVO =  attrService.queryByAttrId(attrId);
        return R.ok().put("attr", attrResponseVO);
    }

    /**
     * 保存属性
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrEntityVO attrEntityVO){
        //attrService.save(attr);
        attrService.saveAttr(attrEntityVO);
        return R.ok("保存成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntityVO attrEntityVO){
		//attrService.updateById(attr);
        attrService.updateByEntity(attrEntityVO);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){

		//attrService.removeByIds(Arrays.asList(attrIds));
        attrService.removeByAttrIds(attrIds);

        return R.ok();
    }

}
